package com.sq.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sq.system.mapper.SysMenuMapper;
import com.sq.system.mapper.SysRoleMenuMapper;
import com.sq.system.pojo.SysMenu;
import com.sq.system.pojo.SysRole;
import com.sq.system.pojo.SysRoleMenu;
import com.sq.system.service.SysMenuService;
import com.sq.system.service.SysRoleMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 *
 */
@Service
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenu>
    implements SysRoleMenuService
{

    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;


    @Override
    public List<Long> selectRoleMenuId(String roleId) {

        List<Long> menuIds = sysRoleMenuMapper.selectAllMenuId(roleId);
        if (Objects.isNull(menuIds)){
            return new ArrayList<Long>();
        }
        return menuIds;
    }

    @Override
    @Transactional
    public boolean dataScopeUpdate(String roleId, List<Integer> menuids) {

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("role_id",roleId);
        sysRoleMenuMapper.delete(queryWrapper);

        List<SysRoleMenu> sysRoleMenus = new ArrayList<>();
        for (Integer menuId : menuids){
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setMenuId(String.valueOf(menuId));
            sysRoleMenu.setRoleId(roleId);
            sysRoleMenus.add(sysRoleMenu);
        }

        return saveBatch(sysRoleMenus);

    }
}




